﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;

namespace BSCMFF.Web.Pages.BSC_Setup.Manager
{
    /// <summary>
    /// Cascade KPIs for (whole) department.
    /// </summary>
    /// <remarks></remarks>
    public partial class CascadingKPI : BSCMFF.Core.MultiLanguagePage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            dkpi.ShowButtons = false;
            dkpi.DisableTextBoxes();            
        }

        /// <summary>
        /// Handles the Click event of the RadButtonCreateLink control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadButtonCreateLink_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();

            foreach (RadListBoxItem i in EGS.RadListBoxSelectedEmployees.Items)
            {
                User u = new User();
                u.ID = Int32.Parse(i.Value);
                u.Lastname = i.Text;

                Personal_KPI pKPI = new Personal_KPI();
                pKPI.Name = "(Personal) " + dkpi.KPI.Name;
                //pKPI.Description = dkpi.KPI.Description;
                pKPI.KPI_FK = dkpi.KPI.KPI_ID;
                pKPI.User_FK = u.ID;
                pKPI.Weight = 1;
                pKPI.TargetValue = 100;
                context.AddToPersonal_KPI(pKPI);
            }

            try
            {
                context.SaveChanges();

                //ShowConnectionTable("(Personal) " + dkpi.KPI.Name, EGS.RadListBoxSelectedEmployees.Items);

                UpdatePKPIs();
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Shows the connection table.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="userItems">The user items.</param>
        /// <remarks></remarks>
        private void ShowConnectionTable(string p, RadListBoxItemCollection userItems)
        {
            BSCEntities context = new BSCEntities();
            List<User> users = new List<User>();
        }

        /// <summary>
        /// Handles the ItemCommand event of the RadGrid1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.GridCommandEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item.ItemIndex != -1) //create new
            {
                int kpiID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["KPI_ID"].ToString());
                
                BSCEntities context = new BSCEntities();

                dkpi.KPI  = (from kpi in context.KPIs
                            where kpi.KPI_ID == kpiID
                            select kpi).SingleOrDefault();
                
                dkpi.DataBind();

                PanelCascade.Visible = true;
                UpdatePKPIs();
            }
        }

        /// <summary>
        /// Handles the NeedDataSource event of the RadGridPKPIs control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.GridNeedDataSourceEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadGridPKPIs_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            UpdatePKPIs();
        }

        /// <summary>
        /// Updates the personal KPIs.
        /// </summary>
        /// <remarks></remarks>
        private void UpdatePKPIs()
        {
            BSCEntities context = new BSCEntities();

            int? selectedKPIID = dkpi.KPI.KPI_ID;

            List<Personal_KPI> pKPIs = (from pkpi in context.Personal_KPI.Include("User")
                                        where pkpi.KPI_FK != null && pkpi.KPI_FK == selectedKPIID
                                        select pkpi).ToList();
            RadGridPKPIs.DataSource = pKPIs;
            RadGridPKPIs.Rebind(); // DataBind();
        }
    }
}